﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ElingOA6_Admin_DSJCJ.Models;
using System.Data;
using ElingOA6_Admin_DSJCJ.Common;
using DevExpress.Web.Mvc;

namespace ElingOA6_Admin_DSJCJ.Areas.Admin.Controllers
{
    public class ChkDocHighEnrolController : Common.AdminBaseController
    {
        //
        // GET: /Admin/ChkDocHighEnrol/

        public ActionResult Index(string year)
        {
            if (year == "" || year == null)
            {
                year = DateTime.Now.Year.ToString();
            }
            ViewData["year"] = year;

            BLL.ChkInfoCls chkInfoAuditManualBLL = new BLL.ChkInfoCls();
            DataSet ds = chkInfoAuditManualBLL.GetYeat(Common.GlobalConstant.ENTITYNO);
            if (ds.Tables[0].Rows.Count == 0)
            {
                ViewData["minyear"] = year;
                ViewData["maxyear"] = year;
            }
            else
            {
                ViewData["maxyear"] = ds.Tables[0].AsEnumerable().Select(t => t.Field<decimal>("FYear")).Max();
                ViewData["minyear"] = ds.Tables[0].AsEnumerable().Select(t => t.Field<decimal>("FYear")).Min();
            }

            ViewData["dqyear"] = year;
            string strwhere = " FYear='" + year + "'";
            List<Model.ChkInfoAuditManual> ChkInfoAuditManualList = GetChkInfoAuditManualListList(strwhere, 1);
           
            return View(ChkInfoAuditManualList);
        }

        public ActionResult ChkDocHighEnrolListPartial()
        {
            int pageIndex = Request.Params["pageIndex"] == null ? 0 : int.Parse(Request.Params["pageIndex"].ToString());
            int pageCount = Request.Params["PageCount"] == null ? 0 : int.Parse(Request.Params["PageCount"].ToString());
            int pageRowSize = Request.Params["pageRowSize"] == null ? 0 : int.Parse(Request.Params["pageRowSize"].ToString());
            int pageRowCount = Request.Params["pageRowCount"] == null ? 0 : int.Parse(Request.Params["pageRowCount"].ToString());
            string callbackArg = DevExpressHelper.CallbackArgument;
            string command = Request.Params["command"] == null ? "" : Request.Params["command"].ToString();

            if (command == "PAGERONCLICK")
            {
                string[] args = callbackArg.Split(new char[1] { '|' });
                string arg = args[args.Count() - 1];

                switch (arg)
                {
                    case "PBN;":
                        {
                            //下一页
                            pageIndex = pageIndex + 2;
                            break;
                        }
                    case "PBP;":
                        {
                            //上一页
                            //pageIndex = pageIndex;
                            break;
                        }
                    case "PBL;":
                        {
                            //最后一页
                            pageIndex = pageCount;
                            break;
                        }
                    case "PBF;":
                        {
                            //第一页
                            pageIndex = 1;
                            break;
                        }
                    default:
                        {
                            //点击数字页码
                            string tempIndex = arg.Replace("PN", "");
                            tempIndex = tempIndex.Replace(";", "");
                            if (RegExp.IsNumeric(tempIndex))
                            {
                                pageIndex = int.Parse(tempIndex) + 1;
                            }
                            break;
                        }

                }
            }
            else
            {
                pageIndex = pageIndex + 1;
            }

            string strwhere = "";
            List<Model.ChkInfoAuditManual> ChkInfoAuditManualList = GetChkInfoAuditManualListList(strwhere, pageIndex);
            return PartialView("ChkDocHighEnrolListPartial", ChkInfoAuditManualList);
        }

        /// <summary>
        /// 获取定义的工作时间数据
        /// </summary>
        /// <param name="strWhere">查询条件</param>
        /// <param name="pageIndex">第几页</param>
        /// <returns>查询出来的数据</returns>
        private List<Model.ChkInfoAuditManual> GetChkInfoAuditManualListList(string strWhere, int pageIndex)
        {
            BLL.Common commonBll = new BLL.Common();
            BLL.ChkInfoAuditManual ChkInfoAuditManualBLL = new BLL.ChkInfoAuditManual();
            string fiedls = "*";
            int total;
            DataSet ds = commonBll.GetListByPage("v_chkInfoAuditManual", fiedls, Common.GlobalConstant.CONSTRING_PAGESIZE, pageIndex, strWhere, "FAuditDate", 1, out total);
            List<ElingOA6_Admin_DSJCJ.Model.ChkInfoAuditManual> ChkInfoAuditManualList = ChkInfoAuditManualBLL.DataTableToList(ds.Tables[0]);
            List<Model.ChkInfoAuditManual> ChkInfoAuditManualAllList = new List<Model.ChkInfoAuditManual>();

            int pageRowSize = Common.GlobalConstant.CONSTRING_PAGESIZE;
            for (int i = 0; i < total; i++)
            {
                ChkInfoAuditManualAllList.Add(null);
            }
            for (int i = 0; i < pageRowSize; i++)
            {
                if (ChkInfoAuditManualList.Count > i)
                {
                    ChkInfoAuditManualAllList[((pageIndex - 1) * pageRowSize) + i] = ChkInfoAuditManualList[i];
                }
            }

            return ChkInfoAuditManualAllList;
        }


        public string GetEmp(string empid)
        {
            try
            {
                BLL.HREmpl HREmplBLL = new BLL.HREmpl();
                string empstr = HREmplBLL.GetEmpNameAndDept(Common.GlobalConstant.ENTITYNO, empid);
                return empstr;
            }
            catch
            {
                return "";
            }
        }

        public string ChkDocHighEnrolDel(string ID)
        {
            try
            {
                BLL.ChkInfoAuditManual ChkInfoAuditManualBLL = new BLL.ChkInfoAuditManual();
                ChkInfoAuditManualBLL.Delete(decimal.Parse(ID));
                return "ok";
            }
            catch
            {
                return "error";
            }
        }

        public string DelChkDocHighEnrolALL(string IDList)
        {
            try
            {
                BLL.ChkInfoAuditManual ChkInfoAuditManualBLL = new BLL.ChkInfoAuditManual();
                ChkInfoAuditManualBLL.DeleteList(IDList);
                return "ok";
            }
            catch
            {
                return "error";
            }
        }

        public ActionResult ChkDocHighEnrolEdit(string year, string fNo)
        {
            
            ViewData["year"] = year;

            Model.ChkInfoAuditManual chkInfoAuditManualModel = new Model.ChkInfoAuditManual();
            BLL.ChkInfoCls bll = new BLL.ChkInfoCls();

            List<SelectListItem> SelectListItemList = new List<SelectListItem>();
            SelectListItem Fitem = new SelectListItem();
            Fitem.Text = Common.GlobalConstant.COMBO_NOCHOOSE_TEXT;
            Fitem.Value = "-2";
            SelectListItemList.Add(Fitem);
            DataSet ds = bll.GetList("FYear =" + year + " and FEntityNo='" + GlobalConstant.ENTITYNO + "'");
            DataTable dt = new DataTable();
            if (ds.Tables.Count != 0)
            {
                dt = ds.Tables[0];
                DataRow[] dr = dt.Select("FPreInfoClsNo is Null or FPreInfoClsNo=''");
                for (int i = 0; i < dr.Length; i++)
                {
                    SelectListItem item = new SelectListItem();
                    item.Text = dr[i]["FInfoClsName"].ToString();
                    item.Value = dr[i]["FInfoClsNo"].ToString() + "-" + dr[i]["FChkType"].ToString();
                    DataRow[] sondr = dt.Select("FPreInfoClsNo='" + dr[i]["FInfoClsNo"] + "'");
                    if (sondr.Count() > 0)
                    {
                        item.Value = "-1";
                    }
                    SelectListItemList.Add(item);
                    GetChildNodeStr(sondr, dt, ref SelectListItemList);
                }
            }

            ViewData["SelectListItem"] = SelectListItemList;
            if (fNo == null || fNo == "")
            {
                chkInfoAuditManualModel.FInfoClsNo = "-2";
                ViewData["type"] = "ADD";
            }
            else
            {
                BLL.ChkInfoAuditManual ChkInfoAuditManualBLL = new BLL.ChkInfoAuditManual();
                chkInfoAuditManualModel = ChkInfoAuditManualBLL.GetModel(decimal.Parse(fNo));
                DataRow[] dr = dt.Select("FInfoClsNo ='" + chkInfoAuditManualModel.FInfoClsNo + "'");
                ViewData["FInfoClsNo"] = chkInfoAuditManualModel.FInfoClsNo;
                chkInfoAuditManualModel.FInfoClsNo = chkInfoAuditManualModel.FInfoClsNo + "-" + dr[0]["FChkType"].ToString();
                ViewData["type"] = "Edit";
            }

            return View("ChkDocHighEnrolEdit", chkInfoAuditManualModel);
        }

        public ActionResult Save(Model.ChkInfoAuditManual model)
        {
            try
            {
                BLL.ChkInfoAuditManual ChkInfoAuditManualBLL = new BLL.ChkInfoAuditManual();
                if (Fetch.Post("FScore") == "计 1 篇")
                {
                    model.FScore = 1;
                }
                model.FEmpNo = Fetch.Post("hdempno");
                model.FDeptNo = Fetch.Post("hddeptno");
                model.FInfoClsNo = Fetch.Post("hidChkInfoNo");
                model.FYear = decimal.Parse(Fetch.Post("hidYear"));
                model.FEntityNo = Common.GlobalConstant.ENTITYNO;
                model.FAuditEmpNo = OperateSession.get(Common.GlobalConstant.PARAM_FEMPLNO);
                model.FAuditDeptNo = OperateSession.get(Common.GlobalConstant.PARAM_DEPTNO);
                switch (Fetch.Post("hdtype"))
                {
                    case "ADD":
                        ChkInfoAuditManualBLL.Add(model);
                        break;
                    case "Edit":
                        model.FInfoID = decimal.Parse(Fetch.Post("hidFInfoID"));
                        ChkInfoAuditManualBLL.Update(model);
                        break;
                }
                ViewData["jsAlert"] = "操作成功";
            }
            catch
            {
                ViewData["jsAlert"] = "操作失败";
            }


            ViewData["year"] = model.FYear;

            Model.ChkInfoAuditManual chkInfoAuditManualModel = new Model.ChkInfoAuditManual();
            BLL.ChkInfoCls bll = new BLL.ChkInfoCls();

            List<SelectListItem> SelectListItemList = new List<SelectListItem>();
            SelectListItem Fitem = new SelectListItem();
            Fitem.Text = Common.GlobalConstant.COMBO_NOCHOOSE_TEXT;
            Fitem.Value = "-2";
            SelectListItemList.Add(Fitem);
            DataSet ds = bll.GetList("FYear =" + model.FYear + " and FEntityNo='" + GlobalConstant.ENTITYNO + "'");
            if (ds.Tables.Count != 0)
            {
                DataTable dt = ds.Tables[0];
                DataRow[] dr = dt.Select("FPreInfoClsNo is Null or FPreInfoClsNo=''");
                for (int i = 0; i < dr.Length; i++)
                {
                    SelectListItem item = new SelectListItem();
                    item.Text = dr[i]["FInfoClsName"].ToString();
                    item.Value = dr[i]["FInfoClsNo"].ToString() + "-" + dr[i]["FChkType"].ToString();
                    DataRow[] sondr = dt.Select("FPreInfoClsNo='" + dr[i]["FInfoClsNo"] + "'");
                    if (sondr.Count() > 0)
                    {
                        item.Value = "-1";
                    }
                    SelectListItemList.Add(item);
                    GetChildNodeStr(sondr, dt, ref SelectListItemList);
                }
            }

            ViewData["SelectListItem"] = SelectListItemList;

            ViewData["type"] = Fetch.Post("hdtype");

            return View("ChkDocHighEnrolEdit", model);
        }

        public void GetChildNodeStr(DataRow[] dr, DataTable dt, ref  List<SelectListItem> SelectListItemList)
        {
            for (int i = 0; i < dr.Length; i++)
            {
                SelectListItem item = new SelectListItem();
                item.Text = "----" + dr[i]["FInfoClsName"].ToString();
                item.Value = dr[i]["FInfoClsNo"].ToString() + "-" + dr[i]["FChkType"].ToString();
                SelectListItemList.Add(item);
                DataRow[] sondr = dt.Select("FPreInfoClsNo='" + item.Value + "'");
                if (sondr.Count() > 0)
                {
                    GetChildNodeStr(sondr, dt, ref SelectListItemList);
                }
            }
        }


        //public ActionResult ChkInfoClsTree(string year)
        //{
        //    TreeModelList treeModelList = new TreeModelList();

        //    TreeItemModel treeParentItemModel = new TreeItemModel(null)
        //    {
        //        Id = "ChkInfoClsAllList?id=-1&year=" + year,
        //        Name = "所有栏目",
        //        ImageUrl = Url.Content("~/Content/ControlImages/scripts_code.png")

        //    };

        //    BLL.ChkInfoCls bll = new BLL.ChkInfoCls();
        //    DataSet ds = bll.GetList("FYear =" + year + " and FEntityNo='" + GlobalConstant.ENTITYNO + "'");
        //    if (ds.Tables.Count != 0)
        //    {
        //        DataTable dt = ds.Tables[0];
        //        DataRow[] dr = dt.Select("FPreInfoClsNo is Null or FPreInfoClsNo=''");
        //        for (int i = 0; i < dr.Length; i++)
        //        {
        //            TreeItemModel treeSonItemModel = new TreeItemModel(treeParentItemModel)
        //            {
        //                Id = dr[i]["FInfoClsNo"].ToString(),
        //                Name = dr[i]["FInfoClsName"].ToString(),
        //                ImageUrl = Url.Content("~/Content/ControlImages/scripts_code.png")

        //            };
        //            GetChildNode(treeSonItemModel, dr[i]["FInfoClsNo"].ToString(), dt, year);
        //        }
        //    }

        //    treeModelList.Add(treeParentItemModel);
        //    return View(treeModelList);
        //}

        ///// <summary>
        ///// 循环得到树节点
        ///// </summary>
        ///// <param name="treeSonItemModel">子节点</param>
        ///// <param name="fInfoClsNo">栏目编号</param>
        ///// <param name="dt">列表</param>
        //private void GetChildNode(TreeItemModel treeSonItemModel, string fInfoClsNo, DataTable dt, string year)
        //{
        //    //string year = Common.OperateSession.get(Common.GlobalConstant.PARAM_SYSYEAR);

        //    DataRow[] dr = dt.Select("FPreInfoClsNo='" + fInfoClsNo + "'");
        //    for (int i = 0; i < dr.Length; i++)
        //    {
        //        TreeItemModel treeItemModel = new TreeItemModel(treeSonItemModel)
        //        {
        //            Id = dr[i]["FInfoClsNo"].ToString(),
        //            Name = dr[i]["FInfoClsName"].ToString(),
        //            ImageUrl = Url.Content("~/Content/ControlImages/scripts_code.png")

        //        };
        //        GetChildNode(treeItemModel, dr[i]["FInfoClsNo"].ToString(), dt, year);

        //    }

        //}


    }
}
